package com.example.smart.controller;

import com.example.common.enums.BusinessType;
import com.example.common.util.R;
import com.example.smart.annotation.Log;
import com.example.smart.service.MenuService;
import com.example.smart.pojo.Menu;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/menu")
@Tag(name = "菜单管理")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @PreAuthorize("hasAuthority('menu:list')")
    @Operation(summary = "获取所有的菜单数据")
    @Log(title = "菜单管理", businessType = BusinessType.SELECT)
    @GetMapping(value = "/getAllMenu")
    public R list() {
        List<Menu> menuList = menuService.list();
        return R.ok("查询所有菜单数据成功").data("menuList", menuList);
    }

    @PreAuthorize("hasAuthority('menu:add')")
    @Operation(summary = "添加菜单")
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @Parameters(value = {
            @Parameter(name = "menuId", description = "菜单编号"),
            @Parameter(name = "menuParentId", description = "父级菜单，一级菜单编号"),
            @Parameter(name = "menuName", description = "菜单名称"),
            @Parameter(name = "menuPath", description = "菜单路径"),
            @Parameter(name = "menuPerms", description = "菜单授权"),
            @Parameter(name = "menuType", description = "菜单类型（0：目录   1：菜单  2：按钮）"),
            @Parameter(name = "menuIcon", description = "菜单图标"),
            @Parameter(name = "orderNum", description = "菜单排序"),
            @Parameter(name = "menuVisible", description = "显示状态（0：显示，1：隐藏）"),
            @Parameter(name = "menuStatus", description = "菜单状态（0：正常，1：停用）"),
            @Parameter(name = "createTime", description = "创建时间"),
            @Parameter(name = "updateTime", description = "修改时间"),
            @Parameter(name = "menuIsdelete", description = "是否删除（是:1，否:0）")
    })
    @PostMapping(value = "/addMenu")
    public R addMenu(@RequestBody Menu Menu) {
        boolean flag = menuService.save(Menu);
        if (flag) {
            return R.ok("菜单添加成功");
        } else {
            return R.error("菜单添加失败");
        }
    }

    @PreAuthorize("hasAuthority('menu:delete')")
    @Operation(summary = "删除菜单")
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    @Parameters(value = {
            @Parameter(name = "menuId", description = "菜单编号", required = true)
    })
    @DeleteMapping(value = "/deleteMenu/{menuId}")
    public R deleteMenu(@PathVariable("menuId") Integer menuId) {
        boolean flag = menuService.removeById(menuId);
        if (flag) {
            return R.ok("菜单删除成功");
        } else {
            return R.error("菜单删除失败");
        }
    }

    @PreAuthorize("hasAuthority('menu:getById')")
    @Operation(summary = "按照菜单编号查询数据")
    @Log(title = "菜单管理", businessType = BusinessType.SELECT)
    @Parameters(value = {
            @Parameter(name = "menuId", description = "菜单编号", required = true)
    })
    @DeleteMapping(value = "/getMenuById/{menuId}")
    public R getByIdMenu(@PathVariable("menuId") Integer menuId) {
        Menu menu = menuService.getById(menuId);
        return R.ok("按照菜单编号查询数据成功").data("menu", menu);
    }

    @PreAuthorize("hasAuthority('menu:update')")
    @PutMapping("/updateMenu")
    @Operation(summary = "修改菜单数据")
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    @Parameters(value = {
            @Parameter(name = "menuId", description = "菜单编号", required = true),
            @Parameter(name = "menuParentId", description = "父级菜单，一级菜单编号"),
            @Parameter(name = "menuName", description = "菜单名称"),
            @Parameter(name = "menuPath", description = "菜单路径"),
            @Parameter(name = "menuPerms", description = "菜单授权"),
            @Parameter(name = "menuType", description = "菜单类型（0：目录   1：菜单  2：按钮）"),
            @Parameter(name = "menuIcon", description = "菜单图标"),
            @Parameter(name = "orderNum", description = "菜单排序"),
            @Parameter(name = "menuVisible", description = "显示状态（0：显示，1：隐藏）"),
            @Parameter(name = "menuStatus", description = "菜单状态（0：正常，1：停用）"),
            @Parameter(name = "createTime", description = "创建时间"),
            @Parameter(name = "updateTime", description = "修改时间"),
            @Parameter(name = "menuIsdelete", description = "是否删除（是:1，否:0）")
    })
    public R updateMenu(@RequestBody Menu menu) {
        boolean flag = menuService.updateById(menu);
        if (flag) {
            return R.ok("修改菜单成功");
        } else {
            return R.error("修改菜单失败");
        }
    }

    /*@GetMapping("/page")
    @Operation(summary = "分页查询菜单")
    @Parameters(value = {
            @Parameter(name = "pageNumber", description = "页码", required = true),
            @Parameter(name = "pageSize", description = "每页大小", required = true)
    })
    public Page< Menu > page(Page< Menu > page) {
        return menuService.page(page);
    }*/
}